/**
 * Created by hasee on 2017/7/4.
 */
var gulp = require('gulp');
var browserify = require('browserify');
var source = require('vinyl-source-stream');
var notify = require('gulp-notify'); // notify error ( X dialog )
var minifyCSS = require('gulp-minify-css');
var uglify = require('gulp-uglify');
var buffer = require('vinyl-buffer');
var watchify = require('watchify');
var rename = require('gulp-rename');
var del = require('del');

var assign = require('object-assign');

var path={
    originDir: "./app",
    main: "./app/index.js",
    html: "./app/*.html",
    css: "./public/css/*.css",
    img: "./public/images/*",
    destDir: "./public",
    destCSS: "./build/css",
    destImg: "./build/img"
};

// ����������Զ��� browserify ѡ��
var customOpts = {
    entries: path.main,
    debug: true,
    "transform": [
        [
            "babelify",
            {
                "presets": [
                    "es2015",
                    "react"
                ]
            }
        ]
    ]
};
var opts = assign({}, watchify.args, customOpts);
// �����Ŀ����ʹ�� watchify ���ӿ� browserify ���ٶȡ��� Rebuild �� 5-10s ���ҽ��� us ����
var b = watchify(browserify(opts));

/** gulp task browserify */

gulp.task( "bundle-dev", ["clean:bundle.*"],() => {

    // ��Ⱦ��g jsx���_���������� transform �\�Пo�`
    return b.transform( "reactify" )

        // ���Йn���ρ��һ���Kָ��Ҫ���� source map. add callback function to print error.
        .bundle( (err, buff) =>
        {
            if( err!= undefined )
            {
                console.log( '[Bundle]', err.toString());
                gulp.src('').pipe( notify('? Bunlde Failed ?') );
                this.emit("end");
            }
        })
        // ���� vinyl-source-stream �͙n��ȡ����
        .pipe( source("bundle.js") )
        //.pipe( gulp.src("bundle.js") )
        // �����ͻص� gulp ϵ�y��ʣ����
        // �@�Y��ֱ�Ӵ�n��Ӳ��
        .pipe( gulp.dest( path.destDir ) )
        .pipe( buffer() )
        .pipe(uglify())
        .pipe(rename({extname:'.min.js'}))
        .pipe(gulp.dest(path.destDir))
});

gulp.task("clean:bundle.*",() =>{
    del(['public/bundle.*']);
   // cb(err);
});

/**
 * Copy files except *.js in app to build( .html, .css, .img and etc );
 */

//gulp.task('copy', function(){
//
//    var html = gulp.src( path.html );
//
//    //MinifyCSS before copy.
//    var css = gulp.src( path.css )
//        .pipe(minifyCSS({
//            noAdvanced: false,
//            keepBreaks:true,
//            cache: true // �@�� gulp ������е�
//        }));
//
//    var img = gulp.src( path.img );
//
//    //copy to dest.
//    css.pipe( gulp.dest( path.destCSS ) );
//    img.pipe( gulp.dest( path.destImg ) );
//    html.pipe( gulp.dest( path.destDir ) );
//})

/**
 * �O�� app/ ������ js, jsx, html, css ׃�������¾��g
 */
gulp.task( 'watch', () => {
    // console.log( 'watch ��' );
    gulp.watch( 'app/**/*', ['bundle-dev'] );
});
//gulp.task( 'watch-public', function() {
//    // console.log( 'watch ��' );
//    gulp.watch( 'public/**/*', ['bundle-dev'] );
//});
//gulp.task( 'bundle-build', function(){
//    return browserify({
//        entries: path.main,
//        debug: true
//    })
//        // ��Ⱦ��g jsx���_���������� transform �\�Пo�`
//        .transform( "reactify" )
//        // ���Йn���ρ��һ���Kָ��Ҫ���� source map. add callback function to print error.
//        .bundle( function(err, buff)
//        {
//            if( err!= undefined )
//            {
//                console.log( '[Bundle]', err.toString());
//                gulp.src('').pipe( notify('? Bunlde Failed ?') );
//                this.emit("end");
//            }
//        })
//        // ���� vinyl-source-stream �͙n��ȡ����
//        .pipe( source("bundle.js") )
//        // need to convert the streaming vinyl file object given by source()
//        // with vinyl-buffer because gulp-uglify (and most gulp plugins) works on buffered vinyl file objects
//        .pipe( buffer() )
//        // then with buff object we can gulp-uglify it
//        .pipe( uglify() )
//        // �����ͻص� gulp ϵ�y��ʣ����
//        // �@�Y��ֱ�Ӵ�n��Ӳ��
//        .pipe( gulp.dest( path.destDir ) )
//});

//build with development. don't uglify .js
//gulp.task('build-dev', ['bundle-dev', 'copy'] );

/**
 * default mode: develop, watch.
 * build mode: just build with min js once.
 */
gulp.task('default', ['bundle-dev', "watch"]);
//gulp.task('build', ['bundle-build', 'copy']);
